/*
 * Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.submarine.common;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.submarine.config.ConfigUtil;
import org.submarine.util.NetUtil;

/**
 * @file org.submarine.common.URL.java
 *
 * URL
 * 
 * @author gaojian
 * @created 2013-8-18 上午00:37:14
 * @version 0.0.1
 * @since 0.0.1
 * 
 */
public class URL {
	
	private final String userName;

	private final String password;

	private final String host;

	private final int port;

	private final String path;
	
	private final String side;

    private final Map<String, String> parameters;
    
    private static final URL localURL = new URL("userName", "password", NetUtil.getHost(), 
    		Integer.parseInt(ConfigUtil.getProperty(Constants.APPLICATION_PORT_KEY)), null, 
    		ConfigUtil.getProperty(Constants.APPLICATION_SIDE_KEY), new HashMap<String, String>());
    
    /**
     * 构造器
     * 
     * @param userName
     * @param password
     * @param host
     * @param port
     * @param path
     * @param parameters
     */
    public URL(String userName, String password, String host, int port, String path, String side, Map<String, String> parameters) {
		if ((userName == null || userName.length() == 0) 
				&& password != null && password.length() > 0) {
			throw new IllegalArgumentException("Invalid url, password without username!");
		}
		this.userName = userName;
		this.password = password;
		this.host = host;
		this.port = (port < 0 ? 0 : port);
		this.path = path;
		this.side = side;
		while(path != null && path.startsWith("/")) {
		    path = path.substring(1);
		}
		if (parameters == null) {
		    parameters = new HashMap<String, String>();
		} else {
		    parameters = new HashMap<String, String>(parameters);
		}
		this.parameters = Collections.unmodifiableMap(parameters);
	}
    
    public static URL localURL() {
    	return localURL;
    }

	public String getUserName() {
		return userName;
	}

	public String getPassword() {
		return password;
	}

	public String getHost() {
		return host;
	}

	public int getPort() {
		return port;
	}

	public String getPath() {
		return path;
	}

	public String getSide() {
		return side;
	}
	
	public String getAddress() {
		return host + ":" + port;
	}

	public Map<String, String> getParameters() {
		return parameters;
	}
	
//    public String getParameter(String key) {
//        return parameters.get(key);
//    }
//
//    public String getParameter(String key, String defaultValue) {
//        String value = getParameter(key);
//        if (value == null || value.length() == 0) {
//            return defaultValue;
//        }
//        return value;
//    }
	
    /**
     * 
     * 将注册路径转化成URL实例
     *
     * @author gaojian
     * @created 2013-8-18 下午6:56:40
     *
     * @param url	//Constants.ROOT_PATH + Constants.FILE_SEPARATOR + side + Constants.FILE_SEPARATOR + url.getHost() + Constants.FILE_SEPARATOR + url.getPort();
     * @return
     */
	public static URL valueOf(String path) {
		String[] values = path.split(Constants.FILE_SEPARATOR);
		String[] vals = values[3].split(Constants.ENGLISH_COLON);
        String userName = "userName";
        String password = "password";
        String host = vals[0];
        int port = Integer.parseInt(vals[1]);
        String side = values[2];
        Map<String, String> parameters = new HashMap<String, String>();
		return new URL(userName, password, host, port, path, side, parameters);
	}

}
